package cn.turboinfo.dongying.api.provider.common.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisBaseSign {


    @Value("${deploy.redis.key-prefix}")
    private String keyPrefix;

    //第七相关
    public static final String DEFAULT_AREA_CODE = "140800";
    public static final Long DEFAULT_AREA_CODE_LONG = 140800L;
    public static final String DEFAULT_AREA_NAME = "运城市";

    // 微信相关
    public static final String WX_OPENID_KEY = "wx:openid:";
    public static final String WX_ACCESS_TOKEN_KEY = "wx:accesstoken:";
    public static final String WX_TICKET_KEY = "wx:ticket";
    public static final String WX_USER_INFO = "wx:userinfo";
    public static final Long ACCESS_TOKEN_EXPIRE = 5400L; // accesstoken存入redis时间

    //分类MAP
    public static final String CATEGORY_RELATIONS = "category:relations";

    // 小程序相关
    public static final String MINI_SESSION = "mini:session:";
    public static final String MINI_LOGIN_STA_MAP = "mini:login:sta";

    public static final String MINI_CREDIT_RATING_TODAY = "mini:credit:rating:today:";

    //数据统计
    public static final String STAT_COMMUNITY_AREA = "stat:community:area";
    public static final String STAT_SHOP_AREA = "stat:shop:area";
    public static final String STAT_CATEGORY_AREA = "stat:category:area";
    public static final String STAT_CATEGORY_SEARCH = "stat:category:search";
    public static final String STAT_SERARCH = "stat:search";

    @Autowired
    RedisUtils redisUtils;

    public static String makeKey(String prex, String body) {
        StringBuffer content = new StringBuffer(prex);
        content.append(body);
        return content.toString();
    }

    public static String makeKey(String... body) {
        StringBuffer content = new StringBuffer();
        for (String b : body) {
            content.append(b);
        }
        return content.toString();
    }

    /**
     * 判断缓存中是否有对应的value
     *
     * @param key
     * @return
     */
    public boolean exists(String key) {
        return redisUtils.exists(key);
    }

    public void rightPushAll(String key, List<Object> value) {
        redisUtils.rightPushAll(key, value);
    }

    public void rightPushAllMap(String key, List<Map> value) {
        redisUtils.rightPushAllMap(key, value);
    }

    public void rightPush(String key, Object value) {
        redisUtils.rightPush(key, value);
    }

    public void leftPushAll(String key, List<Object> value) {
        redisUtils.leftPushAll(key, value);
    }

    public void leftPush(String key, Object value) {
        redisUtils.leftPush(key, value);
    }

    public void hmSet(String key, Map map) {
        redisUtils.hmSet(key, map);
    }

    public Object hmGet(String key, Object hashKey) {
        return redisUtils.hmGet(key, hashKey);
    }

    public void hmSet(String key, Object hashKey, Object value) {
        redisUtils.hmSet(key, hashKey, value);
    }

    public void remove(String key) {
        redisUtils.remove(key);
    }

    public void set(String key, Object value) {
        redisUtils.set(key, value);
    }

    public void set(String key, Object value, Long expireTime) {
        redisUtils.set(key, value, expireTime);
    }

    public void set(String key, Object value, Long expireTime, TimeUnit timeUnit) {
        redisUtils.set(key, value, expireTime, timeUnit);
    }

    public void expire(String key, Long expireTime, TimeUnit timeUnit) {
        redisUtils.expire(key, expireTime, timeUnit);
    }

    public List<Object> getList(String key) {
        return redisUtils.lRange(key, 0, -1);
    }

    public List<Object> hmultlGet(String key, List<String> fields) {
        return redisUtils.hmultlGet(key, fields);
    }

    public Map getMap(String key) {
        return redisUtils.hmGet(key);
    }

    public Object getObject(String key) {
        return redisUtils.get(key);
    }

    public Object brpop(String key) {
        return redisUtils.brpop(key);
    }

    public Object blpop(String key) {
        return redisUtils.blpop(key);
    }

    public String getKey(String key) {
        return keyPrefix + ":" + key;
    }

    public List<Object> getList(String k, long l, long l1) {
        return redisUtils.lRange(k, l, l1);
    }

    public void increaseClickCount(String key, Object value, long count) {
        redisUtils.increaseClickCount(key, value, count);
    }

    public Set<ZSetOperations.TypedTuple<Object>> getTopCategories(String key, int start, int end) {
        return redisUtils.getTopCategories(key, start, end);
    }
}
